home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Extensions / Imaging / PIL / IcoImagePlugin.py < prev    next >
Encoding:
Text File  |  2000-06-23  |  1.7 KB  |  87 lines

  1. #
  2. # The Python Imaging Library.
  3. # $Id: IcoImagePlugin.py,v 1.1.1.1 1998/08/18 13:07:51 sjoerd Exp $
  4. #
  5. # Windows Icon support for PIL
  6. #
  7. # Notes:
  8. #    uses BmpImagePlugin.py to read the bitmap data.
  9. #
  10. # History:
  11. #    96-05-27 fl    Created
  12. #
  13. # Copyright (c) Secret Labs AB 1997.
  14. # Copyright (c) Fredrik Lundh 1996.
  15. #
  16. # See the README file for information on usage and redistribution.
  17. #
  18.  
  19.  
  20. __version__ = "0.1"
  21.  
  22. import string
  23.  
  24. import Image, BmpImagePlugin
  25.  
  26.  
  27. #
  28. # --------------------------------------------------------------------
  29.  
  30. def i16(c):
  31.     return ord(c[0]) + (ord(c[1])<<8)
  32.  
  33. def i32(c):
  34.     return ord(c[0]) + (ord(c[1])<<8) + (ord(c[2])<<16) + (ord(c[3])<<24)
  35.  
  36.  
  37. def _accept(prefix):
  38.     return prefix[:4] == "\0\0\1\0"
  39.  
  40.  
  41. class IcoImageFile(BmpImagePlugin.BmpImageFile):
  42.  
  43.     format = "ICO"
  44.     format_description = "Windows Icon"
  45.  
  46.     def _open(self):
  47.  
  48.     # check magic
  49.     s = self.fp.read(6)
  50.     if not _accept(s):
  51.         raise SyntaxError, "not an ICO file"
  52.  
  53.     # pick the largest icon in the file
  54.     m = ""
  55.     for i in range(i16(s[4:])):
  56.         s = self.fp.read(16)
  57.         if not m:
  58.         m = s
  59.         elif ord(s[0]) > ord(m[0]) and ord(s[1]) > ord(m[1]):
  60.         m = s
  61.         #print "width", ord(s[0])
  62.         #print "height", ord(s[1])
  63.         #print "colors", ord(s[2])
  64.         #print "reserved", ord(s[3])
  65.         #print "planes", i16(s[4:])
  66.         #print "bitcount", i16(s[6:])
  67.         #print "bytes", i32(s[8:])
  68.         #print "offset", i32(s[12:])
  69.  
  70.     # load as bitmap
  71.     self._bitmap(i32(m[12:]))
  72.  
  73.     # patch up the bitmap height
  74.     self.size = self.size[0], self.size[1]/2
  75.     d, e, o, a = self.tile[0]
  76.     self.tile[0] = d, (0,0)+self.size, o, a
  77.  
  78.     return
  79.     
  80.  
  81. #
  82. # --------------------------------------------------------------------
  83.  
  84. Image.register_open("ICO", IcoImageFile, _accept)
  85.  
  86. Image.register_extension("ICO", ".ico")
  87.